package org.mcflier.appuntamento.io.driver.rss.imdb;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

import org.mcflier.appuntamento.core.exception.PropertiesException;
import org.mcflier.appuntamento.core.logger.LoggerManager;
import org.mcflier.appuntamento.core.properties.PropertiesManager;
import org.mcflier.appuntamento.data.model.IMDBList;
import org.mcflier.appuntamento.io.driver.rss.imdb.interfaces.IRssImdbDriver;
import org.mcflier.appuntamento.io.exception.RSSConnectionException;
import org.mcflier.appuntamento.io.exception.RSSParsingException;

public class RssImdbDriver implements IRssImdbDriver {
	
	private static RssImdbDriver instance;
	
	static{
		instance = null;
	}
	
	public static RssImdbDriver getInstance() throws InstantiationException, IllegalAccessException, ClassNotFoundException, PropertiesException{
		if(instance == null)
			instance = new RssImdbDriver();
		
		return instance;
	}
	
	private Logger logger;
	private AbstractRssImdbDriver driver;
	private Properties properties;
	
	private RssImdbDriver() throws InstantiationException, IllegalAccessException, ClassNotFoundException, PropertiesException{
		this.logger = LoggerManager.getInstance().getLogger(this.getClass().getName());
		
		PropertiesManager propertiesManager = PropertiesManager.getInstance();
		this.properties = propertiesManager.getProperties();

		this.logger.info("Loaded properties");
		this.driver = (AbstractRssImdbDriver)Class.forName(this.properties.getProperty(RSS_DRIVER_CLASSPATH)).newInstance();
		this.driver.init(properties);
		this.logger.info("Created rss driver: "+this.driver);
	}

	@Override
	public IMDBList getList(String link) throws RSSConnectionException, RSSParsingException {
		return this.driver.getList(link);
	}

	@Override
	public IMDBList getListById(String idList) throws RSSConnectionException, RSSParsingException {
		return this.driver.getListById(idList);
	}
}